<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace form\element\form;

use form\element\form\traits\Append;
use form\element\form\traits\Placeholder;
use form\element\form\traits\Prepend;
use form\element\form\traits\Value;
use form\element\traits\{Classes,Css,Id,Js};

class Input extends Column
{

    use Prepend, Append,Placeholder,Classes, Id, Css, Js, Value;

    const URL_INPUT            = 'url';
    const TEL_INPUT            = 'tel';
    const TIME_INPUT           = 'time';
    const WEEK_INPUT           = 'week';
    const YEAR_INPUT           = 'year';
    const TEXT_INPUT           = 'text';
    const DATE_INPUT           = 'date';
    const EMAIL_INPUT          = 'email';
    const MONTH_INPUT          = 'month';
    const HIDDNE_INPUT         = 'hidden';
    const DATETIME_INPUT       = 'datetime';
    const PASSWORD_INPUT       = 'password';
    const DATETIME_LOCAL_INPUT = 'datetime-local';
    const RANGE_INPUT          = 'range';
    const COLOR_INPUT          = 'color';
    const NUMBER_INPUT         = 'number';
    /**
     * 默认的inputtype属性
     * @var string
     */
    protected $type = 'text';

    /**
     * 允许通过本类实现的inputtype类型
     * @var string[]
     */
    protected $typeList = [
        'password', 'text', 'email', 'url', 'date', 'datetime', 'datetime-local', 'month', 'year', 'week', 'time', 'tel', 'hidden', 'range', 'color', 'number'
    ];

    protected $numberInputList = ['number', 'range'];
    protected $value;

    protected $max       = 0;
    protected $min       = 0;
    protected $step      = 0;
    protected $maxLength = 0;

    public function maxlength(int $maxLength = 0)
    {
        $this->maxLength = $maxLength;
        return $this;
    }

    public function max(int $max)
    {
        $this->max = $max;
        return $this;
    }

    public function min(int $min)
    {
        $this->min = $min;
        return $this;
    }

    public function step($step)
    {
        $this->step = $step;
        return $this;
    }

    public function type(string $type)
    {
        if (in_array($type, $this->typeList)) {
            $this->type = $type;
        }
        return $this;
    }

    public function value($value)
    {
        $this->value = $value;
        return $this;
    }

    protected function getMin()
    {
        return $this->min > 0 ? 'min=' . $this->min : '';
    }

    protected function getMax()
    {
        return $this->max > 0 ? 'max=' . $this->max : '';
    }

    protected function getStep()
    {
        return $this->step > 0 ? 'step=' . $this->step : '';
    }

    protected function getMaxLength()
    {
        return $this->maxLength > 0 ? ' maxlength="' . $this->maxLength . '"' : '';
    }

    protected function normal(): string
    {
        $label = $this->getLabel();
        $id = $this->getId();
        $class = $this->getClass();
        $autocomplete = $this->getAutocomplete();
        $readonly = $this->getReadonly();
        $disabled = $this->getDisabled();
        $required = $this->getRequired();
        $min = $this->getMin();
        $max = $this->getMax();
        $step = $this->getStep();
        $tips = $this->getTips();
        $prepend = $this->getPrepend();
        $append = $this->getAppend();
        $labelClass = $this->getLabelClass();
        $maxLength = $this->getMaxLength();

        if (empty($prepend) && empty($append)) {
            $input = <<<HTML
<input type="$this->type" name="$this->name" class="form-control" id="$id" placeholder="$this->placeholder" aria-describedby="$id" $autocomplete $readonly $disabled $required value="$this->value" $min $max $step $maxLength/>
HTML;
        } else {
            $input = <<<HTML
<div class="input-group">
    $prepend
    <input type="$this->type" name="$this->name" class="form-control" id="$id" placeholder="$this->placeholder" aria-describedby="$id" $autocomplete $readonly $disabled $required value="$this->value" $min $max $step $maxLength/>
    $append
</div>
HTML;
        }
        return <<<HTML

<div class="$class">
    <label for="$id" class="$labelClass">$label</label>
    $input
    $tips
</div>
HTML;
    }

    protected function grid()
    {

        /*<div class="form-row">
                    <div class="col-7">
                      <input type="text" class="form-control" placeholder="城市">
                    </div>
                    <div class="col">
                      <input type="text" class="form-control" placeholder="状态">
                    </div>
                    <div class="col">
                      <input type="text" class="form-control" placeholder="邮编">
                    </div>
                  </div>*/

    }

    protected function group()
    {
        return $this->normal();
    }

    protected function table()
    {

    }

    protected function vertical()
    {
        $label = $this->getLabel();
        $id = $this->getId();
        $class = $this->getClass();
        $autocomplete = $this->getAutocomplete();
        $readonly = $this->getReadonly();
        $disabled = $this->getDisabled();
        $required = $this->getRequired();
        $tips = $this->getTips();
        $prepend = $this->getPrepend();
        $append = $this->getAppend();
        $labelClass = $this->getLabelClass();

        if (empty($prepend)) {
            $input = <<<HTML
<input type="$this->type" name="$this->name" class="form-control" id="$id" placeholder="$this->placeholder" aria-describedby="$id" $autocomplete $readonly $disabled $required/>
HTML;
        } else {
            $input = <<<HTML
<div class="input-group">
$prepend
<input type="$this->type" name="$this->name" class="form-control" id="$id" placeholder="$this->placeholder" aria-describedby="$id" $autocomplete $readonly $disabled $required/>
$append
</div>
HTML;


        }
        return <<<HTML
<div class="row $class">
    <label for="$id " class="col-sm-2 col-form-label $labelClass">$label</label>
    <div class="col-sm-10 col-lg-10">
        $input
    </div>
    $tips
</div>
HTML;
    }

    protected function inline()
    {

        $label = $this->getLabel();
        $id = $this->getId();
        $class = $this->getClass();
        $autocomplete = $this->getAutocomplete();
        $readonly = $this->getReadonly();
        $disabled = $this->getDisabled();
        $required = $this->getRequired();
        $min = $this->getMin();
        $max = $this->getMax();
        $step = $this->getStep();
        $tips = $this->getTips();
        $prepend = $this->getPrepend();
        $append = $this->getAppend();
        $labelClass = $this->getLabelClass();

        if (empty($prepend) && empty($append)) {
            $input = <<<HTML

<input type="$this->type" name="$this->name" class="form-control" id="$id" placeholder="$this->placeholder" aria-describedby="$id" $autocomplete $readonly $disabled $required $min $max $step/>
HTML;
        } else {
            $input = <<<HTML

<div class="input-group">
    $prepend
    <input type="$this->type" name="$this->name" class="form-control" id="$id" placeholder="$this->placeholder" aria-describedby="$id" $autocomplete $readonly $disabled $required $min $max $step/>
    $append
</div>
HTML;
        }
        return <<<HTML

<div class="$class">
    <label for="$id" class="$labelClass">$label</label>
    $input
    $tips
</div>
HTML;
    }

    public function generate()
    {
        if ($this->type == self::HIDDNE_INPUT) {
            return $this->hiddenInput();
        }
        return parent::generate(); // TODO: Change the autogenerated stub
    }

    protected function hiddenInput()
    {
        return <<<INPUT
    <input type=hidden name="$this->name" value="$this->value" />
INPUT;

    }

}